package myapp.camera;

import android.widget.AbsListView;

public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {

    private int currentPage = 0;
    private int previousTotal = 0;
    public boolean isLoading = true;

    public EndlessScrollListener() {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        if (isLoading) {
            if (totalItemCount > previousTotal) {
                isLoading = false;
                previousTotal = totalItemCount;
                currentPage++;
            }
        }
        //Log.i("onScroll_firstVisibleItem", String.valueOf(firstVisibleItem));
        //Log.i("onScroll_visibleItemCount", String.valueOf(visibleItemCount));
        //Log.i("onScroll_totalItemCount", String.valueOf(totalItemCount));
        //Log.i("onScroll_visibleThreshold", String.valueOf(visibleThreshold));
        boolean loadMore = totalItemCount <= (firstVisibleItem + visibleItemCount);
        if (hasMoreDataToLoad() && !isLoading && loadMore) {
            isLoading = true;
            onLoadMoreData(currentPage);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }
    
	public void onLoadingComplete() {
		isLoading = false;
	}

    protected abstract boolean hasMoreDataToLoad();
    protected abstract void onLoadMoreData(int page);

}
